package com.example.home.park;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.adapter.CatRecordAdapter;
import com.example.home.bean.CatRecordBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.home.tool.StaticActi.ip;

public class RecordActivity extends AppCompatActivity {

    private ImageView back;
    private TextView time_go;
    private TextView time_back;
    private Calendar calendar;
    private Button btu_chaxun;
    private OkHttpClient okHttpClient;
    private Gson gson;
    private Handler handler;
    private Request request;
    private CatRecordBean catRecordBean;
    private CatRecordAdapter catRecordAdapter;
    private RecyclerView rec_list;
    private  String dateTimeStart;
    private  String dateTimeEnd;
    private  Date dateEnd;
    private  Date dateStart;
    private Button btn_more;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        initview();

        getListData(ip+"/prod-api/api/park/lot/record/list");

        btu_chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getRecordListData(ip+"/prod-api/api/park/lot/record/list");
            }
        });

        calendar = Calendar.getInstance();
    }

    private void getListData(String url) {
        request = new Request.Builder()
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                catRecordBean = gson.fromJson(json, CatRecordBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        getBindData();
                    }
                });
            }
        });
    }

    private void getRecordListData(String url) {
        if(dateTimeEnd==null || dateTimeEnd.length()==0){
            //判断是否有值，也就是判断是否已经选择了开始时间
            Toast.makeText(this,"请选择时间",Toast.LENGTH_SHORT).show();
            return;
        }
        if(dateTimeStart==null || dateTimeEnd.length()==0){
            //判断是否有值，也就是判断是否已经选择了开始时间
            Toast.makeText(this,"请选择时间",Toast.LENGTH_SHORT).show();
            return;
        }
        String parames = url + "?entryTime="+dateTimeStart+"&outTime="+dateTimeEnd;
        getListData(parames);
    }

    private void getBindData() {
        if (catRecordBean.getTotal() > 00) {
            catRecordAdapter = new CatRecordAdapter(this, catRecordBean.getRows());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            rec_list.setLayoutManager(linearLayoutManager);
            rec_list.setAdapter(catRecordAdapter);
        }else{
            Toast.makeText(this,"暂无数据",Toast.LENGTH_SHORT).show();
        }

    }

    public void showDateTimePicker(int which) {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.DAY_OF_MONTH, day);

                TimePickerDialog timePickerDialog = new TimePickerDialog(RecordActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @RequiresApi(api = Build.VERSION_CODES.N)
                    @Override
                    public void onTimeSet(TimePicker timePicker, int hour, int minute) {
                        calendar.set(Calendar.HOUR_OF_DAY, hour);
                        calendar.set(Calendar.MINUTE, minute);
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String formattedDateTime = dateFormat.format(calendar.getTime());
                        if(which==1){
                            dateTimeStart= formattedDateTime.toString();
                            try {
                                dateStart = dateFormat.parse(dateTimeStart);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            time_go.setText(dateTimeStart);
                        }else if(which==2){
                            dateTimeEnd = formattedDateTime.toString();
                            try {
                                dateEnd = dateFormat.parse(dateTimeEnd);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            time_back.setText(dateTimeEnd);
                        }

                    }
                }, hour, minute, true);

                timePickerDialog.show();
            }
        }, year, month, day);

        datePickerDialog.show();
    }
    private void initview() {
        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
        rec_list = findViewById(R.id.rec_list);
        time_go = findViewById(R.id.time_go);
        time_back = findViewById(R.id.time_back);
        btn_more = findViewById(R.id.btn_more);

        btn_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当用户点击 "更多" 按钮时，显示所有数据
                if (catRecordAdapter != null) {
                    catRecordAdapter.setShowAllData(true);
                }
            }
        });

        back = findViewById(R.id.back);
        btu_chaxun = findViewById(R.id.btu_chaxun);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        time_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDateTimePicker(1);
            }
        });

        time_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDateTimePicker(2);
            }
        });
    }
}